package com.example.demo53.util.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.demo53.R;
import com.example.demo53.util.been.SettingItem;

import java.util.ArrayList;

public class SettingAdapter extends BaseAdapter {

    private ArrayList<SettingItem> smList;
    Context context;

    public  SettingAdapter(ArrayList<SettingItem> smList, Context context){
        this.smList = smList;
        this.context=context;

    }
    @Override
    public int getCount() {
        return smList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final SettingItem sm=smList.get(position);

        View v;

        ViewHolder holder;

        if(convertView == null)
        {
            v = LayoutInflater.from(context).inflate(R.layout.setting_item_list, null);//动态加载滑入ListView的item的子布局文件，并绘制item view

            /*  缓存子布局文件中的控件对象*/
            holder = new ViewHolder();
            holder.item = (TextView)v.findViewById(R.id.item);
            holder.img = (ImageView)v.findViewById(R.id.img);

            v.setTag(holder);
        }
        //滑入ListView中的item view有缓存
        else
        {
            v = convertView;					        //取出缓存的item View对象
            holder = (ViewHolder)v.getTag();	                        //重新获取ViewHolder对象
        }
//settag gettag 分别可以设置规格外的数据 提取数据
        holder.item.setText(sm.getItem());			//设置fruitName
        holder.img.setImageResource(sm.getImgId());		//设置fruitImage

        //为Button对象添加监听器
//        holder.item.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Toast.makeText(context,"这是" + sm.getItem(), Toast.LENGTH_SHORT).show();
//            }
//        });
        return v;


    }

    class ViewHolder
    {
        ImageView img;
        TextView item;

    }

}
